<?php

namespace App\Modules\Health\Http\Controllers\Admin;

use App\Modules\Health\Repositories\DiseaseScheme;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class DiseaseSchemeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DiseaseScheme(['disease', 'acu']), function (Grid $grid) {
            $grid->model()->orderByDesc('id')->orderBy('day');

            $grid->column('id');
            $grid->column('disease.name', '病症');
            $grid->column('acu.name', '穴位');
            $grid->column('description');
            $grid->column('day');
            $grid->column('time');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('day')->width(2);
                $filter->equal('disease_id', '病症')->select(
                    \App\Modules\Health\Models\Disease::orderByDesc('id')->pluck('name', 'id')->all()
                )->width(3);
                $filter->equal('acu_id', '穴位')->select(
                    \App\Modules\Health\Models\Acupoint::orderByDesc('id')->pluck('name', 'id')->all()
                )->width(3);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DiseaseScheme(), function (Show $show) {
            $show->field('id');
            $show->field('disease_id');
            $show->field('acu_id');
            $show->field('description');
            $show->field('day');
            $show->field('time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DiseaseScheme(), function (Form $form) {
            $form->display('id');
            $form->select('disease_id')->required()->options(
                \App\Modules\Health\Models\Disease::orderByDesc('id')->pluck('name', 'id')->all()
            );
            $form->select('acu_id')->required()->options(
                \App\Modules\Health\Models\Acupoint::orderByDesc('id')->pluck('name', 'id')->all()
            );
            $form->text('description')->required();
            $form->number('day')->required();
            $form->number('time')->required()->help('单位 分钟');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
